﻿using System;
using System.Windows.Media.Imaging;
using SomethingSpacial.CustomControls;
using SomethingSpacial.Model;
using SomethingSpacialClassLib.Web;
using System.Collections.ObjectModel;
using System.ServiceModel.DomainServices.Client;


//I am going to modify this heavily!! in 10 minutes and also the related views... ;)
namespace SomethingSpacial.ViewModel
{
    public class SponsorsViewModel : ViewModelBase
    {
        private SponsorModel MyModel = new SponsorModel();
        private SomethingSpacialContext domainContext;
        private SomethingSpacialContext DomainContext
        {
            get
            {
                if (domainContext == null)
                    domainContext = new SomethingSpacialContext();
                return domainContext;
            }
        }
        LoadOperation LoadSponsors;

        //JL: We provide direct access to the domaincontext.Sponsors Entity Set as everything is already wired up here.
        public EntitySet<SomethingSpacialClassLib.Web.Sponsor> Sponsors
        {
            get
            {
                return (EntitySet<SomethingSpacialClassLib.Web.Sponsor>)DomainContext.Sponsors;
            }
            //No set, it is a read-only property.
        }


        #region "ViewModel Methods"

        public void loadSponsors()
        {
            LoadSponsors = DomainContext.Load(DomainContext.GetSponsorsQuery());
            LoadSponsors.Completed += new EventHandler(LoadSponsors_Completed);
        }

        void LoadSponsors_Completed(object sender, EventArgs e)
        {
            //JL:   This is not needed, as domaincontext.Sponsors gets loaded automatically :)
            //      I leave the old code "by now" until this is validated and accepted (using the entities provided as model).


            //Not sure why this is required.  Wouldn't it have to be "Complete" in order for this Sponsor to be triggered? (ds 09/26/09)
            //if (LoadSponsors.IsComplete == true)
            //{
            //    foreach (Entity entity in LoadSponsors.Entities)
            //    {
            //        Sponsors sponsor = entity as Sponsors;
            //        var userGroupSponsor = new SponsorModel();
            //        userGroupSponsor.SponsorID = sponsor.SPN_Id;
            //        userGroupSponsor.IsActive = sponsor.SPN_IsActive;
            //        userGroupSponsor.LogoImage = sponsor.SPN_LogoFilename;
            //        userGroupSponsor.SponsorDescription = sponsor.SPN_Description;
            //        userGroupSponsor.SponsorName = sponsor.SPN_Name;
            //        userGroupSponsor.SponsorURL = sponsor.SPN_URL;
            //        Sponsors.Add(userGroupSponsor);
            //    }
            //}

        }

        public void addSponsor(Sponsor sponsorNew)
        {
            try
            {
                DomainContext.Sponsors.Add(sponsorNew);
                DomainContext.SubmitChanges();
            }
            catch (Exception exc)
            {
                string message = exc.InnerException.ToString();
                System.Windows.MessageBox.Show(message);
            }
        }

        public void UpdateSponsor()
        {
           DomainContext.SubmitChanges();
        }


        public void DeleteSponsor(Sponsor sponsor)
        {
            try
            {
                DomainContext.Sponsors.Remove(sponsor);
                DomainContext.SubmitChanges();
            }
            catch (Exception exc)
            {
                string message = exc.InnerException.ToString();
                System.Windows.MessageBox.Show(message);
            }

        }        
        
        
        #endregion

        #region "ViewModel Properties"
        public int SponsorID
        {
            get { return MyModel.SponsorID; }
            set
            {
                if (MyModel.SponsorID != value)
                {
                    MyModel.SponsorID = value;
                    InvokePropertyChanged("SponsorID");
                }
            }
        }

        public BitmapImage LogoImage
        {
            get 
            { 
                BitmapImage TempImage = new BitmapImage();
                TempImage.UriSource = new Uri(MyModel.LogoImage, UriKind.RelativeOrAbsolute);
                return TempImage; 
            } 
        }

        public Boolean IsActive
        {
            get { return MyModel.IsActive; }
            set
            {
                if (MyModel.IsActive != value)
                {
                    MyModel.IsActive = value;
                    InvokePropertyChanged("IsActive");
                }
            }
        }

        public string SponsorDescription
        {
            get { return MyModel.SponsorDescription; }
            set
            {
                if (MyModel.SponsorDescription != value)
                {
                    MyModel.SponsorDescription = value;
                    InvokePropertyChanged("SponsorDescription");
                }
            }
        }

        public string SponsorName
        {
            get { return MyModel.SponsorName; }
            set
            {
                if (MyModel.SponsorName != value)
                {
                    MyModel.SponsorName = value;
                    InvokePropertyChanged("SponsorName");
                }
            }
        }

        public string SponsorURL
        {
            get { return MyModel.SponsorURL; }
            set
            {
                if (MyModel.SponsorURL != value)
                {
                    MyModel.SponsorURL = value;
                    InvokePropertyChanged("SponsorURL");
                }
            }
        }

        #endregion
    }
}
